package day20160803;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.*;


public class JsonUtil {
	public void testArrayToJSON(){
        boolean[] boolArray = new boolean[]{true,false,true};  
        JSONArray jsonArray = JSONArray.fromObject( boolArray );  
        System.out.println( jsonArray ); 
      //一般数组转换成JSON
	}
    public void testListToJSON(){
        List list = new ArrayList();  
        list.add( "first" );  
        list.add( "second" );  
        JSONArray jsonArray = JSONArray.fromObject( list );  
        System.out.println( jsonArray );
      //接口对象转换成JSON
    }

	public void testJsonStrToJSON(){
        JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );  
        System.out.println( jsonArray ); 
      //字符串json转换成JSON
	}
	public void testMapToJSON(){
		Map map = new HashMap(); 
	    map.put( "name", "json" );  
	    map.put( "bool", Boolean.TRUE );  
	    map.put( "int", new Integer(1) );  
	    map.put( "arr", new String[]{"a","b"} );  
	    map.put( "func", "function(i){ return this.arr[i]; }" );  
	          
	    JSONObject jsonObject = JSONObject.fromObject( map );  
	    System.out.println( jsonObject );  
	  //Map转换成json， 是用jsonObject
	}
}